package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.TIsvInfo;
import com.ruoyi.business.domain.TUserChannel;
import com.ruoyi.business.service.ITUserChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户通道Controller
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Controller
@RequestMapping("/business/userChannel")
public class TUserChannelController extends BaseController
{
    private String prefix = "business/userChannel";



    @Autowired
    private ITUserChannelService tUserChannelService;

    @RequiresPermissions("business:userChannel:view")
    @GetMapping()
    public String userChannel(ModelMap mmap)
    {
        mmap.put("sysUser", new SysUser());
        mmap.put("isvInfo", new TIsvInfo());
        return prefix + "/userChannel";
    }

    /**
     * 查询用户通道列表
     */
    @RequiresPermissions("business:userChannel:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TUserChannel tUserChannel)
    {
        startPage();
        List<TUserChannel> list = tUserChannelService.selectTUserChannelList(tUserChannel);
        return getDataTable(list);
    }

    /**
     * 导出用户通道列表
     */
    @RequiresPermissions("business:userChannel:export")
    @Log(title = "用户通道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TUserChannel tUserChannel)
    {
        List<TUserChannel> list = tUserChannelService.selectTUserChannelList(tUserChannel);
        ExcelUtil<TUserChannel> util = new ExcelUtil<TUserChannel>(TUserChannel.class);
        return util.exportExcel(list, "用户通道数据");
    }

    /**
     * 新增用户通道
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户通道
     */
    @RequiresPermissions("business:userChannel:add")
    @Log(title = "用户通道", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TUserChannel tUserChannel)
    {
        Long[] userId = Convert.toLongArray(tUserChannel.getUserIds());
        for (int i = 0; i < userId.length; i++) {
            logger.info("新增" + userId[i]);
            tUserChannel.setUserId(userId[i]);
            tUserChannelService.insertTUserChannel(tUserChannel);
        }
        return toAjax(1);
    }

    /**
     * 修改用户通道
     */
    @RequiresPermissions("business:userChannel:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TUserChannel tUserChannel = tUserChannelService.selectTUserChannelById(id);
        mmap.put("tUserChannel", tUserChannel);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户通道
     */
    @RequiresPermissions("business:userChannel:edit")
    @Log(title = "用户通道", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TUserChannel tUserChannel)
    {
        return toAjax(tUserChannelService.updateTUserChannel(tUserChannel));
    }

    /**
     * 删除用户通道
     */
    @RequiresPermissions("business:userChannel:remove")
    @Log(title = "用户通道", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tUserChannelService.deleteTUserChannelByIds(ids));
    }



    @Log(title = "用户收款号状态变更", businessType = BusinessType.UPDATE)
    @RequiresPermissions("business:userChannel:operstatus")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(TUserChannel tUserChannel) {
        TUserChannel newtUserChannel = tUserChannelService.selectTUserChannelById(tUserChannel.getId());
        newtUserChannel.setDisabled(tUserChannel.getDisabled());
        return toAjax(tUserChannelService.updateTUserChannel(newtUserChannel));
//        return toAjax(tNginxInfoService.updateTNginxInfo(tNginxInfo));
    }
}
